package com.kun.permiss.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;

/**
 * @author fangkun
 * @create 2023-05-15-9:53
 *
 * 同时赋值角色和权限，登陆成功之后我们获取用户权限，只返回权限，因为用户被覆盖掉了，
 * 这两个只能一个起作用
 */
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig {

    @Bean
    UserDetailsService us(){
        InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
        manager.createUser(User.withUsername("fk").password("{noop}123")
               .roles("admin")
               .authorities("system:user:add","system:user:delete")
//                .authorities("system:user:*")
               .build());
        return manager;
    }

    @Bean
    CustomMethodSecurityExpressionHandler customMethodSecurityExpressionHandler(){
        return new CustomMethodSecurityExpressionHandler();
    }

    @Bean
    SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .permitAll();
        return http.build();
    }
}
